package drops.dao;


import java.math.BigDecimal;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Date;
import java.util.Collections;
import java.util.List;

import org.apache.commons.lang.ObjectUtils;
import org.apache.torque.TorqueException;
import org.apache.torque.om.BaseObject;
import org.apache.torque.om.ComboKey;
import org.apache.torque.om.DateKey;
import org.apache.torque.om.NumberKey;
import org.apache.torque.om.ObjectKey;
import org.apache.torque.om.SimpleKey;
import org.apache.torque.om.StringKey;
import org.apache.torque.om.Persistent;
import org.apache.torque.util.Criteria;
import org.apache.torque.util.Transaction;

  
  
/**
 * This class was autogenerated by Torque on:
 *
 * [Fri Mar 19 07:17:38 SGT 2010]
 *
 * You should not use this class directly.  It should not even be
 * extended all references should be to DentalTransaction
 */
public abstract class BaseDentalTransaction extends BaseObject
{
    /** The Peer class */
    private static final DentalTransactionPeer peer =
        new DentalTransactionPeer();

        
    /** The value for the transactionNumber field */
    private int transactionNumber;
      
    /** The value for the patientNumber field */
    private int patientNumber;
      
    /** The value for the date field */
    private Date date;
      
    /** The value for the description field */
    private String description;
      
    /** The value for the debit field */
    private double debit;
      
    /** The value for the creditDate field */
    private Date creditDate;
      
    /** The value for the creditAmount field */
    private double creditAmount;
      
    /** The value for the balance field */
    private double balance;
  
    
    /**
     * Get the TransactionNumber
     *
     * @return int
     */
    public int getTransactionNumber()
    {
        return transactionNumber;
    }

                        
    /**
     * Set the value of TransactionNumber
     *
     * @param v new value
     */
    public void setTransactionNumber(int v) 
    {
    
                  if (this.transactionNumber != v)
              {
            this.transactionNumber = v;
            setModified(true);
        }
    
          
              }
  
    /**
     * Get the PatientNumber
     *
     * @return int
     */
    public int getPatientNumber()
    {
        return patientNumber;
    }

                              
    /**
     * Set the value of PatientNumber
     *
     * @param v new value
     */
    public void setPatientNumber(int v) throws TorqueException
    {
    
                  if (this.patientNumber != v)
              {
            this.patientNumber = v;
            setModified(true);
        }
    
                          
                if (aPatient != null && !(aPatient.getPatientNumber() == v))
                {
            aPatient = null;
        }
      
              }
  
    /**
     * Get the Date
     *
     * @return Date
     */
    public Date getDate()
    {
        return date;
    }

                        
    /**
     * Set the value of Date
     *
     * @param v new value
     */
    public void setDate(Date v) 
    {
    
                  if (!ObjectUtils.equals(this.date, v))
              {
            this.date = v;
            setModified(true);
        }
    
          
              }
  
    /**
     * Get the Description
     *
     * @return String
     */
    public String getDescription()
    {
        return description;
    }

                        
    /**
     * Set the value of Description
     *
     * @param v new value
     */
    public void setDescription(String v) 
    {
    
                  if (!ObjectUtils.equals(this.description, v))
              {
            this.description = v;
            setModified(true);
        }
    
          
              }
  
    /**
     * Get the Debit
     *
     * @return double
     */
    public double getDebit()
    {
        return debit;
    }

                        
    /**
     * Set the value of Debit
     *
     * @param v new value
     */
    public void setDebit(double v) 
    {
    
                  if (this.debit != v)
              {
            this.debit = v;
            setModified(true);
        }
    
          
              }
  
    /**
     * Get the CreditDate
     *
     * @return Date
     */
    public Date getCreditDate()
    {
        return creditDate;
    }

                        
    /**
     * Set the value of CreditDate
     *
     * @param v new value
     */
    public void setCreditDate(Date v) 
    {
    
                  if (!ObjectUtils.equals(this.creditDate, v))
              {
            this.creditDate = v;
            setModified(true);
        }
    
          
              }
  
    /**
     * Get the CreditAmount
     *
     * @return double
     */
    public double getCreditAmount()
    {
        return creditAmount;
    }

                        
    /**
     * Set the value of CreditAmount
     *
     * @param v new value
     */
    public void setCreditAmount(double v) 
    {
    
                  if (this.creditAmount != v)
              {
            this.creditAmount = v;
            setModified(true);
        }
    
          
              }
  
    /**
     * Get the Balance
     *
     * @return double
     */
    public double getBalance()
    {
        return balance;
    }

                        
    /**
     * Set the value of Balance
     *
     * @param v new value
     */
    public void setBalance(double v) 
    {
    
                  if (this.balance != v)
              {
            this.balance = v;
            setModified(true);
        }
    
          
              }
  
      
    
                  
    
        private Patient aPatient;

    /**
     * Declares an association between this object and a Patient object
     *
     * @param v Patient
     * @throws TorqueException
     */
    public void setPatient(Patient v) throws TorqueException
    {
            if (v == null)
        {
                    setPatientNumber(0);
                  }
        else
        {
            setPatientNumber(v.getPatientNumber());
        }
                aPatient = v;
    }

                                            
    /**
     * Get the associated Patient object
     *
     * @return the associated Patient object
     * @throws TorqueException
     */
    public Patient getPatient() throws TorqueException
    {
        if (aPatient == null && (this.patientNumber > 0))
        {
                          aPatient = PatientPeer.retrieveByPK(SimpleKey.keyFor(this.patientNumber));
              
            /* The following can be used instead of the line above to
               guarantee the related object contains a reference
               to this object, but this level of coupling
               may be undesirable in many circumstances.
               As it can lead to a db query with many results that may
               never be used.
               Patient obj = PatientPeer.retrieveByPK(this.patientNumber);
               obj.addDentalTransactions(this);
            */
        }
        return aPatient;
    }

    /**
     * Provides convenient way to set a relationship based on a
     * ObjectKey.  e.g.
     * <code>bar.setFooKey(foo.getPrimaryKey())</code>
     *
           */
    public void setPatientKey(ObjectKey key) throws TorqueException
    {
      
                        setPatientNumber(((NumberKey) key).intValue());
                  }
       
                
    private static List fieldNames = null;

    /**
     * Generate a list of field names.
     *
     * @return a list of field names
     */
    public static synchronized List getFieldNames()
    {
        if (fieldNames == null)
        {
            fieldNames = new ArrayList();
              fieldNames.add("TransactionNumber");
              fieldNames.add("PatientNumber");
              fieldNames.add("Date");
              fieldNames.add("Description");
              fieldNames.add("Debit");
              fieldNames.add("CreditDate");
              fieldNames.add("CreditAmount");
              fieldNames.add("Balance");
              fieldNames = Collections.unmodifiableList(fieldNames);
        }
        return fieldNames;
    }

    /**
     * Retrieves a field from the object by name passed in as a String.
     *
     * @param name field name
     * @return value
     */
    public Object getByName(String name)
    {
          if (name.equals("TransactionNumber"))
        {
                return new Integer(getTransactionNumber());
            }
          if (name.equals("PatientNumber"))
        {
                return new Integer(getPatientNumber());
            }
          if (name.equals("Date"))
        {
                return getDate();
            }
          if (name.equals("Description"))
        {
                return getDescription();
            }
          if (name.equals("Debit"))
        {
                return new Double(getDebit());
            }
          if (name.equals("CreditDate"))
        {
                return getCreditDate();
            }
          if (name.equals("CreditAmount"))
        {
                return new Double(getCreditAmount());
            }
          if (name.equals("Balance"))
        {
                return new Double(getBalance());
            }
          return null;
    }
    
    /**
     * Retrieves a field from the object by name passed in
     * as a String.  The String must be one of the static
     * Strings defined in this Class' Peer.
     *
     * @param name peer name
     * @return value
     */
    public Object getByPeerName(String name)
    {
          if (name.equals(DentalTransactionPeer.TRANSACTION_NUMBER))
        {
                return new Integer(getTransactionNumber());
            }
          if (name.equals(DentalTransactionPeer.PATIENT_NUMBER))
        {
                return new Integer(getPatientNumber());
            }
          if (name.equals(DentalTransactionPeer.DATE))
        {
                return getDate();
            }
          if (name.equals(DentalTransactionPeer.DESCRIPTION))
        {
                return getDescription();
            }
          if (name.equals(DentalTransactionPeer.DEBIT))
        {
                return new Double(getDebit());
            }
          if (name.equals(DentalTransactionPeer.CREDIT_DATE))
        {
                return getCreditDate();
            }
          if (name.equals(DentalTransactionPeer.CREDIT_AMOUNT))
        {
                return new Double(getCreditAmount());
            }
          if (name.equals(DentalTransactionPeer.BALANCE))
        {
                return new Double(getBalance());
            }
          return null;
    }

    /**
     * Retrieves a field from the object by Position as specified
     * in the xml schema.  Zero-based.
     *
     * @param pos position in xml schema
     * @return value
     */
    public Object getByPosition(int pos)
    {
            if (pos == 0)
        {
                return new Integer(getTransactionNumber());
            }
              if (pos == 1)
        {
                return new Integer(getPatientNumber());
            }
              if (pos == 2)
        {
                return getDate();
            }
              if (pos == 3)
        {
                return getDescription();
            }
              if (pos == 4)
        {
                return new Double(getDebit());
            }
              if (pos == 5)
        {
                return getCreditDate();
            }
              if (pos == 6)
        {
                return new Double(getCreditAmount());
            }
              if (pos == 7)
        {
                return new Double(getBalance());
            }
              return null;
    }
     
    /**
     * Stores the object in the database.  If the object is new,
     * it inserts it; otherwise an update is performed.
     *
     * @throws Exception
     */
    public void save() throws Exception
    {
          save(DentalTransactionPeer.getMapBuilder()
                .getDatabaseMap().getName());
      }

    /**
     * Stores the object in the database.  If the object is new,
     * it inserts it; otherwise an update is performed.
       * Note: this code is here because the method body is
     * auto-generated conditionally and therefore needs to be
     * in this file instead of in the super class, BaseObject.
       *
     * @param dbName
     * @throws TorqueException
     */
    public void save(String dbName) throws TorqueException
    {
        Connection con = null;
          try
        {
            con = Transaction.begin(dbName);
            save(con);
            Transaction.commit(con);
        }
        catch(TorqueException e)
        {
            Transaction.safeRollback(con);
            throw e;
        }
      }

      /** flag to prevent endless save loop, if this object is referenced
        by another object which falls in this transaction. */
    private boolean alreadyInSave = false;
      /**
     * Stores the object in the database.  If the object is new,
     * it inserts it; otherwise an update is performed.  This method
     * is meant to be used as part of a transaction, otherwise use
     * the save() method and the connection details will be handled
     * internally
     *
     * @param con
     * @throws TorqueException
     */
    public void save(Connection con) throws TorqueException
    {
          if (!alreadyInSave)
        {
            alreadyInSave = true;


  
            // If this object has been modified, then save it to the database.
            if (isModified())
            {
                if (isNew())
                {
                    DentalTransactionPeer.doInsert((DentalTransaction) this, con);
                    setNew(false);
                }
                else
                {
                    DentalTransactionPeer.doUpdate((DentalTransaction) this, con);
                }
            }

                      alreadyInSave = false;
        }
      }


                    
      /**
     * Set the PrimaryKey using ObjectKey.
     *
     * @param  transactionNumber ObjectKey
     */
    public void setPrimaryKey(ObjectKey key)
        
    {
            setTransactionNumber(((NumberKey) key).intValue());
        }

    /**
     * Set the PrimaryKey using a String.
     *
     * @param key
     */
    public void setPrimaryKey(String key) 
    {
            setTransactionNumber(Integer.parseInt(key));
        }

  
    /**
     * returns an id that differentiates this object from others
     * of its class.
     */
    public ObjectKey getPrimaryKey()
    {
          return SimpleKey.keyFor(getTransactionNumber());
      }

 

    /**
     * Makes a copy of this object.
     * It creates a new object filling in the simple attributes.
       * It then fills all the association collections and sets the
     * related objects to isNew=true.
       */
      public DentalTransaction copy() throws TorqueException
    {
        return copyInto(new DentalTransaction());
    }
  
    protected DentalTransaction copyInto(DentalTransaction copyObj) throws TorqueException
    {
          copyObj.setTransactionNumber(transactionNumber);
          copyObj.setPatientNumber(patientNumber);
          copyObj.setDate(date);
          copyObj.setDescription(description);
          copyObj.setDebit(debit);
          copyObj.setCreditDate(creditDate);
          copyObj.setCreditAmount(creditAmount);
          copyObj.setBalance(balance);
  
                    copyObj.setTransactionNumber(0);
                                                      
        
        return copyObj;
    }

    /**
     * returns a peer instance associated with this om.  Since Peer classes
     * are not to have any instance attributes, this method returns the
     * same instance for all member of this class. The method could therefore
     * be static, but this would prevent one from overriding the behavior.
     */
    public DentalTransactionPeer getPeer()
    {
        return peer;
    }

    public String toString()
    {
        StringBuffer str = new StringBuffer();
        str.append("DentalTransaction:\n");
        str.append("TransactionNumber = ")
           .append(getTransactionNumber())
           .append("\n");
        str.append("PatientNumber = ")
           .append(getPatientNumber())
           .append("\n");
        str.append("Date = ")
           .append(getDate())
           .append("\n");
        str.append("Description = ")
           .append(getDescription())
           .append("\n");
        str.append("Debit = ")
           .append(getDebit())
           .append("\n");
        str.append("CreditDate = ")
           .append(getCreditDate())
           .append("\n");
        str.append("CreditAmount = ")
           .append(getCreditAmount())
           .append("\n");
        str.append("Balance = ")
           .append(getBalance())
           .append("\n");
        return(str.toString());
    }
}
